package cn.sms.product.service.impl;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.service.impl.BaseServiceImpl;
import cn.sms.org.domain.Employee;
import cn.sms.org.mapper.EmployeeMapper;
import cn.sms.product.domain.Product;
import cn.sms.product.domain.ProductDetail;
import cn.sms.product.mapper.ProductMapper;
import cn.sms.product.service.IProductDetailService;
import cn.sms.product.service.IProductService;
import cn.sms.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IProductDetailService productDetailService;

    @Override
    public Product queryByIdDetail(Long productId) {
        return productMapper.queryByIdDetail(productId);
    }

    @Override
    public void handle(Product product, Logininfo loginInfo) {
        Employee employee = employeeMapper.ByLogininfoId(loginInfo.getId());
        if(employee.getShop_id() == null){
            throw new BusinessException("您不能添加");
        }
        product.setShop_id(employee.getShop_id());
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());

        productDetailService.add(detail);
    }

    @Override
    public void onsale(List<Long> ids) {
        Map map = new HashMap<>();
        map.put("ids", ids);
        productMapper.onsale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        Map map = new HashMap<>();
        map.put("ids", ids);
        productMapper.offSale(map);
    }
}
